package org.example;
import java.util.Random;
/**
 * 加减法口算练习系统-v0.2
 * 支持答案存储，保证加法和不超过100，减法差不小于0
 */
public class CalculationSystemV02 {
    // 存储算式和答案的数据结构
    static class Question {
        String expression;
        int answer;
        Question(String expression, int answer) {
            this.expression = expression;
            this.answer = answer;
        }
    }
    public static void main(String[] args) {
        System.out.println("加减法口算练习系统-v0.2");
        System.out.println("========================");
        Random random = new Random();
        int totalQuestions = 50;
        Question[] questions = new Question[totalQuestions];
        // 生成题目
        for (int i = 0; i < totalQuestions; i++) {
            boolean isAddition = random.nextBoolean();
            int num1, num2, answer;
            String expression;
            if (isAddition) {
                // 加法：确保和不超过100
                num2 = random.nextInt(100);
                num1 = random.nextInt(100 - num2 + 1);
                answer = num1 + num2;
                expression = num1 + " + " + num2 + " = ";
            } else {
                // 减法：确保差不小于0
                num1 = random.nextInt(100);
                num2 = random.nextInt(num1 + 1);
                answer = num1 - num2;
                expression = num1 + " - " + num2 + " = ";
            }
            questions[i] = new Question(expression, answer);
        }
        // 显示题目（每行4个）
        for (int i = 0; i < totalQuestions; i++) {
            System.out.printf("%-18s", questions[i].expression);
            if ((i + 1) % 4 == 0) {
                System.out.println();
            }
        }
        // 显示答案（可选）
        System.out.println("\n\n答案：");
        System.out.println("=====");
        for (int i = 0; i < totalQuestions; i++) {
            System.out.printf("%-18s%d", questions[i].expression, questions[i].answer);
            if ((i + 1) % 2 == 0) {
                System.out.println();
            }
        }
        System.out.println("\n========================");
    }
}